package com.ygu.ecare.activity;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;


import com.ygu.ecare.utils.HTTPGetTool;
import com.ygu.ecare.utils.URLUtils;
import com.ygu.ecare.R;

public class Login extends Activity {
	private EditText username,password;
	private Button login;
	private ProgressDialog prodialog;
	private TextView failmessage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		login = (Button) findViewById(R.id.loginbtn);
		username =  (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		failmessage = (TextView) findViewById(R.id.failmessage);
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//如果用户名为空弹出提示
				login();
			}

			//Handler UIHandler = new Handler(){
				
			//};
		});
	}
	public void login(){
		if(username.getText().toString().trim().equals("")){
			failmessage.setText("用户名不能为空");
			return;
		}else{
		prodialog = ProgressDialog.show(Login.this, "", "登录中，请稍后");
		Thread login = new LoginThread();
		login.start();
		}
		
	}
	public class LoginThread extends Thread{
		public void loginUnsuccess(String msg){
			Message unsuccessmsg = new Message();
			Bundle bundle = new Bundle();
			bundle.putInt("type", 0);
			bundle.putString("msg", msg);
			unsuccessmsg.setData(bundle);
			UIHandler.sendMessage(unsuccessmsg);
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String uname = username.getText().toString().trim();
			String passwd = password.getText().toString().trim();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("userid", uname));
			params.add(new BasicNameValuePair("password", passwd));
			JSONObject res = HTTPGetTool.getTool().post(URLUtils.HOSTMAIN+URLUtils.URLLOGIN, params);
				 try {
					if (res == null)
					{
						loginUnsuccess("连接服务器失败");
					}
					else if (res.get("loginInfo").equals("LOGIN_USERNOTEXIST"))
					{
						loginUnsuccess("用户名不存在");
					}
					else if (res.get("loginInfo").equals("LOGIN_PASSWORD_ERROR"))
					{
						loginUnsuccess("密码错误,请注意大小写");
					}
					else if (res.get("loginInfo").equals("LOGIN_NOTSUCCESS"))
					{
						loginUnsuccess("登录不成功");
					}
					
					else{

						Intent intent = new Intent();
						intent.setClass(Login.this, ChooseHospital.class);
						startActivity(intent);
						prodialog.dismiss();

					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}

		}
		Handler UIHandler = new Handler(){
			public void handleMessage (Message msg) {
				int type = msg.getData().getInt("type");
				switch(type){
				case 0:
					failmessage.setText(msg.getData().getCharSequence("msg"));
					prodialog.dismiss();  
				}
			};
		};
	}
}
